package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"os/exec"
	"syscall"
)

func ExeFun(name string, arg ...string) string {
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	fmt.Println(name, arg)
	cmd := exec.Command(name, arg...)
	cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		fmt.Println(err)
		if err.Error() != "exit status 1" {
			fmt.Printf("error: %v\n", err)
		}
	}
	str := stderr.String()
	if len(str) == 0 {
		str = stdout.String()
	}
	return str
}
func main1() {
	go ExeFun(
		"cmd.exe",
		"/c",
		fmt.Sprintf(`start /b %s/db/OedisDB.exe %s/db/c.conf`,
			SystemDir, SystemDir))

	Ser = RedisInit("127.0.0.1:3208", "o0n.cn", 0)
	Controller{}.Init()
	err := http.ListenAndServe(":8880", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err.Error())
	}

}

/*
   nohup ./blog >blog.log 2>&1 & tail -f blog.log

   SET CGO_ENABLED=0
   SET GOOS=linux
   SET GOARCH=amd64
   go build

*/
type dollars float32
type MyHandler map[string]dollars

func (self MyHandler) list(w http.ResponseWriter, req *http.Request) {
	for item, price := range self {
		fmt.Fprintf(w, "%s: %s\n", item, price)
	}
}
func (self MyHandler) price(w http.ResponseWriter, req *http.Request) {
	item := req.URL.Query().Get("item")
	price, ok := self[item]
	if !ok {
		w.WriteHeader(http.StatusNotFound) // 404
		fmt.Fprintf(w, "no such item: %q\n", item)
		return
	}
	fmt.Fprintf(w, "%s\n", price)
}

func main() {
	handler := MyHandler{"shoes": 50, "socks": 5}
	mux := http.NewServeMux()
	mux.Handle("/list", http.HandlerFunc(handler.list))
	mux.Handle("/list", http.HandlerFunc(handler.list))
	mux.Handle("/price", http.HandlerFunc(handler.price))
	log.Fatal(http.ListenAndServe("localhost:8000", mux))
}
